package org.cliff.message.platform.core.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.cliff.message.platform.core.domain.*;
import org.cliff.message.platform.core.domain.*;
import org.cliff.message.platform.core.mapper.ReceivedMessageResendDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author 刘文
 * @date 2023/10/26
 */
@Service
public class ReceivedMessageResendService {

    @Autowired
    private ReceivedMessageResendDetailMapper resendDetailMapper;

    public void updateStatusToTriggered(ReceivedMessageResendDetail resendDetail) {
        LambdaUpdateWrapper<ReceivedMessageResendDetail> updateWrapper = Wrappers.lambdaUpdate(ReceivedMessageResendDetail.class)
                .set(ReceivedMessageResendDetail::getSendStatus, SendStatusConstants.TRIGGERED)
                .eq(ReceivedMessageResendDetail::getId, resendDetail.getId());
        resendDetailMapper.update(null, updateWrapper);
    }

    /**
     * 重发完成之后更新重发记录状态
     * @param resendDetail
     */
    public void updateRecordWhenResendFinished(ReceivedMessageResendDetail resendDetail) {
        LambdaUpdateWrapper<ReceivedMessageResendDetail> updateWrapper = Wrappers.lambdaUpdate(ReceivedMessageResendDetail.class)
                .set(ReceivedMessageResendDetail::getSendStatus, resendDetail.getSendStatus())
                .set(ReceivedMessageResendDetail::getRequestTime, resendDetail.getRequestTime())
                .set(ReceivedMessageResendDetail::getResponseTime, resendDetail.getResponseTime())
                .set(ReceivedMessageResendDetail::getStatusCode, resendDetail.getStatusCode())
                .set(ReceivedMessageResendDetail::getProcessTime, resendDetail.getProcessTime())
                .set(ReceivedMessageResendDetail::getResponseDetail, resendDetail.getResponseDetail())
                .eq(ReceivedMessageResendDetail::getId, resendDetail.getId());
        resendDetailMapper.update(null, updateWrapper);
    }

    /**
     * 自动重发指定接收到的消息
     * @param receivedMessage
     * @param interfaceConfig
     * @param curRetryCount
     */
    public void autoResendMessage(ReceivedMessage receivedMessage, InterfaceConfig interfaceConfig, int curRetryCount) {
        // 超过重试次数，放弃重试
        if (curRetryCount > interfaceConfig.getAutoRetryTimes()) {
            return;
        }

        ReceivedMessageResendDetail resendDetail = new ReceivedMessageResendDetail();
        resendDetail.setMessageId(receivedMessage.getId());
        resendDetail.setSendType(SendTypeConstants.AUTO_RESEND);
        resendDetail.setSendStatus(SendStatusConstants.PENDING);
        resendDetail
                .setPlanedSendTime(interfaceConfig.getAutoRetryInterval() * 1000 + System.currentTimeMillis());
        resendDetail.setAutoRetryCount(curRetryCount + 1);
        resendDetailMapper.insert(resendDetail);
    }
}
